/**
 * zhuoyuan93@gmail.com
 * 屏幕工具类 以及一些常用的工具类封装
 * ui设计基准,iphone 6
 * width:750px
 * height:1334px
 * @2x
 */
import {
  PixelRatio,
  Dimensions,
  Platform,
} from 'react-native';

export let screenW = Dimensions.get('window').width;
export let screenH = Dimensions.get('window').height;
export let pixelRatio = PixelRatio.get();
// 像素密度
export const DEFAULT_DENSITY = 2;
// px转换成dp
// 以iphone6为基准,如果以其他尺寸为基准的话,请修改下面的750和1334为对应尺寸即可.
const w2 = 750 / DEFAULT_DENSITY;
// px转换成dp
const h2 = 1334 / DEFAULT_DENSITY;

// iPhoneX XS
const X_WIDTH = 375;
const X_HEIGHT = 812;

// iphoneXR XS Max
const XR_WIDTH = 414;
const XR_HEIGHT = 896;

export default {
  width: Dimensions.get('window').width,
  height: Dimensions.get('window').height,
  onePixel: 1 / PixelRatio.get(),
  isIOS: Platform.OS === 'ios',
  statusBarHeight: (Platform.OS === 'ios' ? 20 : 0),
};


/**
 * 设置字体的size（单位px）
 * @param size 传入设计稿上的px
 * @returns {Number} 返回实际sp
 */
export function fontScale(sizePx) {
  let scaleWidth = screenW / w2;
  let scaleHeight = screenH / h2;
  let scale = Math.min(scaleWidth, scaleHeight);
  let size = Math.round((sizePx * scale + 0.5));
  return size / DEFAULT_DENSITY;
}

/**
 * 屏幕适配,缩放size
 * @param size
 * @returns {Number}
 */
export function scaleSize(sizePx) {
  let scaleWidth = screenW / w2;
  let scaleHeight = screenH / h2;
  let scale = Math.min(scaleWidth, scaleHeight);
  let size = Math.round((sizePx * scale + 0.5));
  return size / 2;
}

/**
 * 判断是否为iphoneX XS
 * @returns {boolean}
 */
export function isIphoneX() {
  return (
    Platform.OS === 'ios' &&
    ((screenH === X_HEIGHT && screenW === X_WIDTH) ||
      (screenH === X_WIDTH && screenW === X_HEIGHT))
  );
}

//判断是否为iphoneXR或XsMAX
export function isIphoneXR() {
  return (
      Platform.OS === 'ios' && 
      ((screenH === XR_HEIGHT && screenW === XR_WIDTH) || 
      (screenH === XR_WIDTH && screenW === XR_HEIGHT))
  )
}
// 获取iPhone状态栏高度
export function isIphoneXTop(){
  if(Platform.OS === 'ios' &&( isIphoneX() || isIphoneXR())){
    return 44;
  }
  if(Platform.OS === 'ios'){
    return 20;
  }
  return 0;
}

// 获取iPhoneX底部高度
export function isIphoneXFooter(){
  if(Platform.OS === 'ios' &&( isIphoneX() || isIphoneXR())){
    return 34;
  }
  return 0;
}



/**
 * 根据是否是iPhoneX返回不同的样式
 * @param iphoneXStyle
 * @param iosStyle
 * @param androidStyle
 * @returns {*}
 */
export function ifIphoneX(iphoneXStyle, iosStyle = {}, androidStyle) {
  if (isIphoneX()) {
    return iphoneXStyle;
  } else if (Platform.OS === 'ios') {
    return iosStyle;
  } else {
    if (androidStyle) {
      return androidStyle;
    }
    return iosStyle;
  }
}

/**
 * 适配宽度
 * @param {*} sizePx
 */
export function autoWidth(sizePx) {
  let scaleWidth = screenW / w2;
  return Math.round(sizePx * scaleWidth + 0.5) / DEFAULT_DENSITY;
}

/**
 * 适配高度
 * @param {*} sizePx
 */
export function autoHeigth(sizePx) {
  let scaleHeight = screenH / h2;
  return Math.round(sizePx * scaleHeight + 0.5) / DEFAULT_DENSITY;
}
